//
//  MenuListViewController.m
//  Happy Kitchen
//
//  Created by mac_chen on 15/3/7.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "MenuListViewController.h"

@interface MenuListViewController ()

@end

@implementation MenuListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = self.therapyDetail.diseaseName;
    
    
    
    // 瀑布流类
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 2 - 10, self.view.frame.size.height / 3);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 7, 10, 7);
    
    // 集合视图
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 90, self.view.frame.size.width, self.view.frame.size.height - 49 - 90 - 64) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    // 指定代理人
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.view addSubview:self.collectionView];
    
    
    // 创建显示小知识视图
    DescribeView *describeView = [[DescribeView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 90) diseaseDescribe:self.diseaseDescribe];
    
    // 指定代理人
    describeView.myDelegate = self;
    
    [self.view addSubview:describeView];
    
    
    //设置下拉刷新状态
    self.isUpLoading = NO;
    
    //调用刷新方法
    [self addHeader];
    [self addFooter];
    
    //注册cell
    [self.collectionView registerClass:[MenuListCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    //初始化数组,字典
    self.myArray = [NSMutableArray array];
    self.myDic = [NSMutableDictionary dictionary];
    
    
    
}
// 实现代理方法
- (void)modalTextDetailView
{
    TextDetailViewController *textDetaiVC = [[TextDetailViewController alloc] init];
    
    textDetaiVC.diseaseDescribe = self.diseaseDescribe;
    textDetaiVC.fitEat = self.fitEat;
    textDetaiVC.lifeSuit = self.lifeSuit;
    
    [self presentViewController:textDetaiVC animated:YES completion:^{
        
        
    }];
    
}

//自定义方法,请求数据
- (void)getMenuListDetail:(NSString *)nextPage
{
    
    NSString *url_Str = [NSString stringWithFormat:@"http://42.121.13.106:8080/HandheldKitchen/api/vegetable/tbldisease!getVegetable.do?diseaseId=%@&page=%@&pageRecord=10&phonetype=0&is_traditional=0", self.diseaseId, nextPage];
    
    [GetAFN downLoadBlock:url_Str block:^(id result) {
        
        if (self.isUpLoading == NO) {
            //拉下刷新,清除数组的所有数据,重新刷新请求数据
            [self.myArray removeAllObjects];
        }
        self.myDic = result;
        NSMutableArray *array = [NSMutableArray arrayWithArray:[self.myDic objectForKey:@"data"]];
        
        for (NSDictionary *dic in array) {
            MenuList *menuList = [[MenuList alloc] init];
            menuList.diseaseDescribe = self.diseaseDescribe;
            [menuList setValuesForKeysWithDictionary:dic];
            
            [self.myArray addObject:menuList];
            
        }
        NSMutableDictionary *dictionary = [self.myDic objectForKey:@"bean"];
        
        self.nextCursor = [dictionary objectForKey:@"nextPageNumber"];//记录下一页
        
        
        //刷新collectionView
        [self.collectionView reloadData];
        //结束上拉刷新,下拉加载
        [self.collectionView footerEndRefreshing];
        [self.collectionView headerEndRefreshing];
        
    }];
    
    
}


//下拉刷新
- (void)addHeader
{
    __block MenuListViewController *menuList = self;
    
    //添加下拉刷新头部控件
    [self.collectionView addHeaderWithCallback:^{
        //进入刷新状态回调
        
        menuList.isUpLoading = NO;//标记下拉操作
        [menuList getMenuListDetail:[NSString stringWithFormat:@"1"]];//请求数据
        
    }];
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.collectionView headerBeginRefreshing];
    
}
#pragma mark --上拉加载更多
- (void)addFooter
{
    __block MenuListViewController *menuList = self;
    
    //添加上拉控件
    [self.collectionView addFooterWithCallback:^{
        
        //进入刷新状态回调
        menuList.isUpLoading = YES;//标记上拉操作
        [menuList getMenuListDetail:menuList.nextCursor];//请求更多
        
    }];
}

//必须需要的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.myArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MenuListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    //用数组初始化model
    MenuList *menuList = [self.myArray objectAtIndex:indexPath.row];
    
    //sd方法请求图片
    NSURL *url = [NSURL URLWithString:menuList.imagePathThumbnails];
    [cell.imagePathThumbnailsImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    [cell setMenuList:menuList];
    
    return cell;
}

//点击cell进入详情页面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MenuDetailViewController *menuDetailVC = [[MenuDetailViewController alloc] init];
    
    UINavigationController *menuDetailNaVC = [[UINavigationController alloc] initWithRootViewController:menuDetailVC];
    //设置导航栏不透明
    menuDetailNaVC.navigationBar.translucent = NO;
    //初始化model
    MenuList *menuList = [self.myArray objectAtIndex:indexPath.row];
    //通过push传值
    menuDetailVC.vegetable_id = menuList.vegetableId;
    menuDetailVC.name = menuList.name;
    [self.navigationController pushViewController:menuDetailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
